package processorIO;

import java.io.File;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import stringFunctions.StringFunctions;

/**
 * A class for splitting file paths into its directory component, file name
 * component, and file extension component.
 *
 * Fully Tested: Yes
 * Fully Documented: Yes
 * Created: Nov 7, 2011
 * Last Modified: Nov 7, 2011
 *
 * @author Shimu Wu
 */
public class FilePath {

    private String fullPath;

    private String directoryPath, fileName, extension;

    /**
     * Instantiates a FilePath, from which the directory component, file name
     * component, and file extension component can be retrieved via invocation
     * of getters.
     *
     * @param file_path the full path
     */
    public FilePath(String file_path) {
        this.fullPath = file_path;
        split(file_path);
    }

    public FilePath(String directory, String fileName, String extension){
        this.directoryPath = directory;
        this.fileName = fileName;
        this.extension = extension;
        join (directoryPath, fileName, extension);
    }

    /**
     * Splits the file_path into directories, fileName, and extension;
     *
     * @param file_path the full path
     */
    private void split(String file_path) {
        // Split the file_path first into directories, and file name
        Pattern p = Pattern.compile("([^/]*)/([^/]*)");
        Matcher m = p.matcher(file_path);
        String name;
        if (m.matches()) {
            this.directoryPath = m.group(1);
            name = m.group(2);
        } else {
            this.directoryPath = "";
            name = file_path;
        }

        // Further split file name into name and file extension
        p = Pattern.compile("([^\\.]*)\\.([^\\.]*)");
        m = p.matcher(name);
        if (m.matches()) {
            this.fileName = m.group(1);
            this.extension = m.group(2);
        } else {
            this.fileName = name;
            this.extension = "";
        }
    }

    /**
     * Joins the given directory, fileName, and extension into a full path.
     * Full path = directory/fileName.extension
     *
     * @param directory the directory path
     * @param fileName the file name
     * @param extension the file extension
     */
    private void join(String directory, String fileName, String extension){

        if (!directory.isEmpty()){
            this.fullPath = directory + "/" + fileName;
        } else {
            this.fullPath = fileName;
        }

        if (!extension.isEmpty()){
            this.fullPath += "." + extension;
        }
    }

    /**
     * Returns the full path
     * @return the full path
     */
    public String getFullPath(){
        return fullPath;
    }

    /**
     * Returns the path of the directories in this file path.
     * Returns the empty String ("") if this file path does not
     * contain any directories.
     * 
     * @return the path of the directories, the empty String ("") if
     * no directories exist
     */
    public String getDirectoryPath() {
        return directoryPath;
    }

    /**
     * Returns the name of the file denoted by this file path.
     *
     * @return the name of the file, the empty String ("") if the full
     * path is the empty String ("")
     */
    public String getFileName() {
        return fileName;
    }

    /**
     * Returns the extension of the file denoted by this file path.
     *
     * @return the extension, the empty String ("") if no extension exists
     */
    public String getExtension() {
        return extension;
    }
    
    /**
     * Join the given set of paths together using File.separator.
     * 
     * @param path
     * @return 
     */
    public static String join(String... path) {
        return StringFunctions.insertSeparator(Arrays.asList(path), 
                File.separator).replace(File.separator + File.separator, File.separator);
    }
    
    public static String getName(String path) {
        return new File(path).getName();
    }
}
